#include "torusWindow.h"
#include "BRep_Tool.hxx"


torusWindow::torusWindow(modelSoft* myModelSoft, TorusFeature* m_torusFeature, QWidget *parent)
	:myModelSoft(myModelSoft)
	,torusFeature(m_torusFeature)
	,QWidget(parent)
	, ui(new Ui::torusWindowClass())
{
	ui->setupUi(this);
	ui->radius1Input->setText(QString::number(torusFeature->getRadius1()));
	ui->radius2Input->setText(QString::number(torusFeature->getRadius2()));
	shapeID = torusFeature->getShapeId();
	//查找该Pipeline是否已存在，存在则获取，不存在则创建
	auto iter1 = myModelSoft->m_pipelineMap.find(torusFeature->getShapeId());
	//Handle(QIVtkSelectionPipeline) cubePipeline;
	if (iter1 == myModelSoft->m_pipelineMap.end()) {
		torusPipeline = myModelSoft->makeTorus(torusFeature);
		torusPipeline->Actor()->SetPickable(false);
	}
	else {
		torusPipeline = iter1->second;
		//if()
		edit = true;
		//已存在pipeline,高亮显示
		torusPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->highlightProperty);
		myModelSoft->myView->getRenderWindow()->Render();
	}
	createConnections();
}

void torusWindow::createConnections() {
	//connect(ui->cancelButton, SIGNAL(&QPushButton::clicked), this, SLOT(close()));
	//上面这种连接方式为何不行???
	connect(ui->confirmButton, &QPushButton::clicked, this, &torusWindow::makeTorus_onClick);
	connect(ui->cancelButton, &QPushButton::clicked, this, &QWidget::close);
	connect(ui->vertexSelectButton, &QPushButton::clicked, this, &torusWindow::onClickVertexSelect);
	connect(ui->radius1Input, &QLineEdit::textChanged, this, &torusWindow::onRadius1Changed);
	connect(ui->radius2Input, &QLineEdit::textChanged, this, &torusWindow::onRadius2Changed);
	connect(myModelSoft->currVertexSelector, &OccVertexSelector::sentVertex, this, &torusWindow::receivePos);

}

void torusWindow::closeEvent(QCloseEvent* event)
{
	if (!confirmCreate) {
		if (!torusPipeline.IsNull() && !edit) {
			myModelSoft->RemovePipelineFromRenderer(torusPipeline);
			myModelSoft->myView->getRenderWindow()->Render();
		}
	}
	if (!torusPipeline.IsNull() && edit) {
		torusPipeline->Actor()->GetProperty()->DeepCopy(myModelSoft->myView->getInteractorStyle()->baseProperty);
		myModelSoft->myView->getRenderWindow()->Render();
	}
}

void torusWindow::receivePos(const TopoDS_Shape& shape)
{
	const TopoDS_Vertex& vertex = TopoDS::Vertex(shape);
	// 获取顶点坐标
	if (!vertex.IsNull()) {
		point_pos = BRep_Tool::Pnt(vertex);
		torusFeature->setLocation(point_pos);
		torusPipeline = myModelSoft->updateTorusPipelineShape(torusPipeline, torusFeature);
		torusPipeline->Actor()->SetPickable(false);
	}
	else {
		qDebug() << "null vertex";
	}
}

void torusWindow::onClickVertexSelect()
{
	myModelSoft->myView->getInteractorStyle()->setSelectionMode(SM_Vertex);
	myModelSoft->changeSelector(SM_Vertex);
	myModelSoft->myView->getInteractorStyle()->setCurrSelector(myModelSoft->currSelector);

}

void torusWindow::onRadius1Changed()
{
	torusFeature->setRadius1(ui->radius1Input->text().toDouble());
	torusPipeline = myModelSoft->updateTorusPipelineShape(torusPipeline, torusFeature);
	torusPipeline->Actor()->SetPickable(false);
}

void torusWindow::onRadius2Changed()
{
	torusFeature->setRadius2(ui->radius2Input->text().toDouble());
	torusPipeline = myModelSoft->updateTorusPipelineShape(torusPipeline, torusFeature);
	torusPipeline->Actor()->SetPickable(false);

}

double torusWindow::get_torus_radius1() {
	QString lengthText = ui->radius1Input->text();
	return lengthText.toDouble();
}

double torusWindow::get_torus_radius2() {
	QString widthText = ui->radius2Input->text();

	return widthText.toDouble();
}

void torusWindow::makeTorus_onClick() {
	confirmCreate = true;
	torusPipeline->Actor()->SetPickable(true);
	
	if(!edit){
	QStandardItem* item1 = new QStandardItem(QString("圆环体-%1").arg(torusPipeline->GetShapeID()));
	int row = myModelSoft->ui->model->rowCount();
	item1->setEditable(false);
	myModelSoft->ui->model->setItem(row, 0, item1);
	QStandardItem* item2 = new QStandardItem(QString("隐藏"));
	item2->setCheckable(true);
	myModelSoft->ui->model->setItem(row, 1, item2);
	}
	close();
}

torusWindow::~torusWindow()
{}
